package controller

import (
	"github.com/gin-gonic/gin"
	"log"
	"myserver/model"
	"myserver/param"
	"myserver/response"
	"myserver/service"
	"strconv"
)

type WorkController struct {
}

var workService = service.WorkService{}

func (c *WorkController) Router(apiGroup *gin.RouterGroup) {
	apiGroup.GET("/work", c.gets)
	apiGroup.GET("/works", c.getWorks)
	apiGroup.POST("/work", c.posts)
	apiGroup.DELETE("/work", c.deletes)
	apiGroup.PUT("/work", c.puts)
	apiGroup.GET("/userWorks", c.userWorks)
}

func (c *WorkController) gets(context *gin.Context) {
	var work model.Work
	id, b := context.GetQuery("ID")
	if !b {
		//response.Failed(context, response.Error().AddData("msg", "参数解析失败"))
		//return
		log.Println(b)
	}
	workId, _ := strconv.ParseUint(id, 10, 64)
	work.ID = uint(workId)
	res := workService.GetWork(work)
	if res.ID != 0 {
		response.Success(context, response.Ok().AddData("res", res))
	} else {
		response.Failed(context, response.Error().AddData("res", res))
	}
}

func (c *WorkController) posts(context *gin.Context) {
	var work model.Work
	err := context.ShouldBind(&work)
	if err != nil {
		response.Failed(context, response.Error().AddData("msg", "参数解析失败"))
	}
	res := workService.CreateWork(work)
	if res != 0 {
		response.Success(context, response.Ok().AddData("res", res))
	} else {
		response.Failed(context, response.Error().AddData("res", res))
	}
}

func (c *WorkController) deletes(context *gin.Context) {
	var work model.Work
	context.ShouldBind(&work)
	if work.ID != 0 {
		response.Failed(context, response.Error().AddData("msg", "参数解析失败"))
	}
	res := workService.DeleteWork(work)
	if res != 0 {
		response.Success(context, response.Ok().AddData("res", res))
	} else {
		response.Failed(context, response.Error().AddData("res", res))
	}
}

func (c *WorkController) puts(context *gin.Context) {
	var work model.Work
	err := context.ShouldBind(&work)
	if err != nil {
		response.Failed(context, response.Error().AddData("msg", "参数解析失败"))
	}
	res := workService.UpdateWork(work)
	if res != 0 {
		response.Success(context, response.Ok().AddData("res", res))
	} else {
		response.Failed(context, response.Error().AddData("res", res))
	}
}

func (c *WorkController) getWorks(context *gin.Context) {
	var pageParam param.PageParam
	err := context.ShouldBind(&pageParam)
	if err != nil {
		response.Failed(context, response.Error().AddData("msg", "参数解析失败"))
	}
	res := workService.GetWorkPage(pageParam)
	if res.Data != nil && res.Total != 0 {
		response.Success(context, response.Ok().AddData("res", res))
	} else {
		response.Failed(context, response.Ok().AddData("res", res))
	}
}

func (c *WorkController) userWorks(context *gin.Context) {
	var pageParam param.PageParam
	err := context.ShouldBind(&pageParam)
	if err != nil {
		response.Failed(context, response.Error().AddData("msg", "参数解析失败"))
	}
	res := workService.UserWorks(pageParam)
	if res.Total != 0 {
		response.Success(context, response.Ok().AddData("works", res.Data).AddData("total", res.Total))
	}else {
		response.Failed(context, response.Error())
	}
}
